'''
Desc: 实现坚果类
Author: 陈爱国
Date: 2024-08-24 09:00:30
'''
import pygame

class WallNut(pygame.sprite.Sprite):
    def __init__(self):
        # 调用父类构造方法
        super(WallNut, self).__init__()
        # 定义对象图片
        self.image = pygame.image.load('material/images/WallNut_00.png').convert_alpha()
        # 图片序列，用于产生动画
        self.images = [pygame.image.load('material/images/WallNut_{:02d}.png'.format(i)).convert_alpha() for i in range(13)]
        # 坚果被破坏图片序列
        self.crackedImages = [
            pygame.transform.smoothscale(pygame.image.load("material/images/Wallnut_body.png").convert_alpha(),
                                         (self.image.get_rect().width, self.image.get_rect().height)),
            pygame.transform.smoothscale(pygame.image.load("material/images/Wallnut_cracked1.png").convert_alpha(),
                                         (self.image.get_rect().width, self.image.get_rect().height)),
            pygame.transform.smoothscale(pygame.image.load("material/images/Wallnut_cracked2.png").convert_alpha(),
                                         (self.image.get_rect().width, self.image.get_rect().height)),
        ]
        # 图片的矩形区域
        self.rect = self.images[0].get_rect()
        # 图片top
        self.rect.top = 200
        # 图片left
        self.rect.left = 250
        # 血量
        self.energy = 8 * 15
        # 存储碰到的僵尸
        self.zombies = set()

    def update(self, *args):
        # 未被僵尸破坏的坚果
        if self.energy==8*15:
            self.image = self.images[args[0] % len(self.images)]
        elif 6*15<=self.energy<8*15:
            # 被僵尸部分破坏的坚果
            self.image = self.crackedImages[0]
        elif 3*15<=self.energy<6*15:
            # 被僵尸部分破坏的坚果
            self.image = self.crackedImages[1]
        else:
            self.image = self.crackedImages[2]

        if self.energy <= 0:
            # 坚果消失
            self.kill()
            # 挡住的僵尸状态改变
            for zombie in self.zombies:
                zombie.isMeetWallNut = False